<!-- 种树管理 -->
<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="display: flex;justify-content: space-between;align-items: center;padding: 0 15px;">
			<div>
				<p class="text">种树管理</p>
			</div>
			<el-form :inline="true" :model="filters" style="margin-top: 12px;">
				<el-form-item>
					<el-input v-model="filters.text" placeholder="请输入手机号"></el-input>
				</el-form-item>
				<el-form-item >
					<el-button type="primary" icon="el-icon-search" v-on:click="getList()">查询</el-button>
					<el-button type="primary"  @click="dialogVisible = true" >分发红包</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<el-table :data="List" highlight-current-row v-loading="loading" border :cell-style="cellStyle" :header-cell-style="rowClass" style="width: 100%;">
			<el-table-column prop="id" align="center" label="ID" width="80"></el-table-column>
			<el-table-column prop="treeName" align="center" label="名称"></el-table-column>
			<el-table-column prop="treeLevel" align="center" label="等级"></el-table-column>
			<el-table-column prop="proportion" align="center" label="命中概率"></el-table-column>
			<el-table-column prop="redPackage" align="center" label="红包金额"></el-table-column>
			<el-table-column prop="treeCount" align="center" label="最大领取人数">
				<template slot-scope="scope">
					<p v-if="scope.row.treeCount == '0'">无限人数</p>
					<p v-else>{{scope.row.treeCount}}</p>
				</template>
			</el-table-column>
			<el-table-column label="操作" width="200">
				<template slot-scope="scope">
					<el-button type="primary" size="small" @click="LookUser(scope.$index, scope.row)" icon="el-icon-view">查看用户</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!-- 查看用户 -->
		<el-dialog title="树用户预设列表" :visible.sync="UserShow" width="50%" :before-close="handleClose">
			<el-col :span="24" class="toolbar" style="display: flex;justify-content: flex-end;padding-bottom: 0;">
				<el-form :inline="true">
					<el-form-item >
						<el-button type="primary" @click="adddialogVisible = true" icon="el-icon-plus">添加用户</el-button>
					</el-form-item>
					<!-- <el-form-item >
						<el-button type="primary" @click="Inquire(0)" icon="el-icon-search">查询用户</el-button>
					</el-form-item> -->
				</el-form>
			</el-col>
			<el-table :data="UserList" highlight-current-row v-loading="loading" border :cell-style="cellStyle" :header-cell-style="rowClass" style="width: 100%;">
				<el-table-column prop="id" align="center" label="ID" width="80"></el-table-column>
				<el-table-column prop="realName" align="center" label="姓名"></el-table-column>
				<el-table-column prop="phone" align="center" label="手机号"></el-table-column>
				<el-table-column prop="ctime" align="center" label="时间" width="180"></el-table-column>
				<el-table-column label="操作" width="200">
					<template slot-scope="scope">
						<el-button type="danger" size="small" @click="delUser(scope.$index, scope.row)" icon="el-icon-delete">删除用户</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!--批量删除-->
			<el-col :span="24" class="toolbar">
				<el-pagination background layout="total,sizes,prev, pager, next,jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
				</el-pagination>
			</el-col>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="UserShow = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="loading">确定</el-button>
			</div>
		</el-dialog>
		<!-- 添加用户 -->
		<el-dialog title="添加用户" :visible.sync="adddialogVisible" width="73%" :before-close="handleClose">
			<el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
				<el-form-item label="ID" prop="treeId">
					<el-input v-model="addForm.treeId" auto-complete="off" readonly></el-input>
				</el-form-item>
				<el-form-item label="手机号" prop="phone">
					<el-input v-model="addForm.phone" auto-complete="off" maxlength="11" placeholder="请输入手机号"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="adddialogVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="loading">提交</el-button>
			</div>
		</el-dialog>
		<!-- 分段显示 -->
		<el-dialog title="账号信息" :visible.sync="AccountVisible" width="73%" :before-close="handleClose">
			<h1>账号基本信息</h1>
			<el-form :model="AccountForm" label-width="100px" ref="AccountForm">
				<el-form-item label="最高级别" prop="treeLevel">
					<el-input v-model="AccountForm.treeLevel" auto-complete="off" readonly></el-input>
				</el-form-item>
				<el-form-item label="账号类型" prop="isService">
					<el-input v-if="AccountForm.isService == '0'" v-model="name0" auto-complete="off" readonly></el-input>
					<el-input  v-else-if ="AccountForm.isService == '1'" v-model="name1" auto-complete="off" readonly></el-input>
				</el-form-item>
				<el-form-item label="水滴量" prop="waterAmount">
					<el-input v-model="AccountForm.waterAmount" auto-complete="off" readonly></el-input>
				</el-form-item>
				<el-form-item label="财神进度" prop="mammonProportion">
					<el-input v-model="AccountForm.mammonProportion" auto-complete="off" readonly></el-input>
				</el-form-item>
			</el-form>
			<el-table :data="AccountList" highlight-current-row v-loading="loading" border :cell-style="cellStyle" :header-cell-style="rowClass" style="width: 100%;">
				<el-table-column prop="treeId" align="center" label="ID" width="80"></el-table-column>
				<el-table-column prop="treeName" align="center" label="树名称"></el-table-column>
				<el-table-column prop="treeLevel" align="center" label="树级别"></el-table-column>
				<el-table-column prop="treeSortno" align="center" label="树位置"></el-table-column>
			</el-table>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="AccountVisible = false">取消</el-button>
				<el-button type="primary" @click.native="AccountSubmit" :loading="loading">更改账号类型</el-button>
			</div>
		</el-dialog>
		<!-- 分发红包 -->
		<el-dialog title="分发红包" :visible.sync="dialogVisible" width="73%" :before-close="handleClose">
			<el-form :model="Form" label-width="100px" :rules="FormRules" ref="Form">
				<el-form-item label="红包金额" prop="money">
					<el-input v-model="Form.money" auto-complete="off" placeholder="请输入红包金额"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="dialogVisible = false">取消</el-button>
				<el-button type="primary" @click.native="Envelope" :loading="loading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>
<script>
	import {
		getTreeList,
		getTreeUserList,
		addTreeUser,
		delTreeUser,
		getUserAccountInfo,
		editUserAccountMode,
		getRedPackage,
		Refreshtoken
	} from '../../../api/api';
	
	export default {
		data() {
			return {
				filters: {
					text: ''
				},
				loading: false,
				List: [],
				UserList:[],//树用户列表
				sels: [],//列表选中列81560
				total: 0,
				page: 1,
				size:10,
				status:0,
				timer: null,
				id:'',
				UserShow:false,
				dialogVisible:false,
				Form:{money:'',},
				FormRules:{
					money: [{
						required: true,
						message: '请输入红包金额',
						trigger: 'blur'
					}],
				},
				adddialogVisible:false,
				addForm:{phone:'',},
				addFormRules:{
					phone: [{
						required: true,
						message: '请输入手机号',
						trigger: 'blur'
					}],
				},
				AccountVisible:false,
				AccountForm:{},
				name0:'普通',
				name1:'服务商',
				AccountList:[]
			}
		},
		mounted() {
			this.TreeList()
			this.queryInfo();
			this.timer = setInterval(() => {
			   setTimeout(this.queryInfo, 0)
		    }, 600000)
		},
		methods: {
			//获取列表
			TreeList:function(type){
				var Token = sessionStorage.getItem('Token');
				var timestamp = new Date().getTime();
				var para = {token: Token,curpage:this.page,pagesize:this.size,timestamp: timestamp};
				this.loading = true;
				getTreeList(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.total = res.data.total;
						this.List = res.data;
						this.loading = false;
					}
				});
			},
			// 设置定时器
			queryInfo() {
				var Token = sessionStorage.getItem('Token');
				var timestamp = new Date().getTime();
				let para = {token: Token,timestamp: timestamp};
				Refreshtoken(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.loading = false;
					}
				});
			},
			//通过手机号查询
			getList(){
				if(this.filters.text == ''){
					this.$message({
						message: '请输入查询条件',
						type: 'error'
					});
				}else{
					this.AccountVisible = true
					var Token = sessionStorage.getItem('Token');
					var timestamp = new Date().getTime();
					var para = {token: Token,curpage:this.page,pagesize:this.size,timestamp: timestamp,phone:this.filters.text};
					this.loading = true;
					getUserAccountInfo(para).then((res) => {
						var res = res.data;
						if (res.code != 0) {
							this.$message({
								message: res.message,
								type: 'error'
							});
						} else {
							this.AccountList = res.data.treeList;
							this.AccountForm = res.data.account
							this.loading = false;
						}
					});
				}
			},
			AccountSubmit(){
				if (this.AccountForm.isService == '0') {
					this.$confirm('确认修改用户为服务商类型吗？', '提示', {}).then(() => {
						this.loading = true;
						var Token = sessionStorage.getItem('Token');
						var timestamp = new Date().getTime();
						var para = {token: Token,userId:this.AccountForm.id,isService:1,timestamp: timestamp};
						this.loading = true;
						editUserAccountMode(para).then((res) => {
							var res = res.data;
							if (res.code != 0) {
								this.$message({
									message: res.message,
									type: 'error'
								});
							} else {
								this.loading = false;
								this.$message({
									message: '修改成功',
									type: 'success'
								});
								this.$refs['AccountForm'].resetFields();
								this.AccountVisible = false;
							}
						});
					});
				} else if(this.AccountForm.isService == '1'){
					this.$confirm('确认修改用户为普通类型吗？', '提示', {}).then(() => {
						this.loading = true;
						var Token = sessionStorage.getItem('Token');
						var timestamp = new Date().getTime();
						var para = {token: Token,userId:this.AccountForm.id,isService:0,timestamp: timestamp};
						this.loading = true;
						editUserAccountMode(para).then((res) => {
							var res = res.data;
							if (res.code != 0) {
								this.$message({
									message: res.message,
									type: 'error'
								});
							} else {
								this.loading = false;
								this.$message({
									message: '修改成功',
									type: 'success'
								});
								this.$refs['AccountForm'].resetFields();
								this.AccountVisible = false;
							}
						});
					});
				}
			},
			Inquire(type){
				if (type == '1') {
					this.status = 1
					this.getGMSList()
				} else if(type == '2'){
					this.status = 2
					this.getGMSList()
				}else if(type == '0'){
					this.status = 0
					this.getGMSList()
				}
			},
			// 查看用户
			LookUser:function(index, row){
				var Token = sessionStorage.getItem('Token');
				var timestamp = new Date().getTime();
				this.id = row.id
				this.addForm.treeId = row.id
				var para = {token: Token,timestamp: timestamp,treeId:row.id,curpage:this.page,pagesize:this.size};
				getTreeUserList(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.$message({
							type: 'success',
							message: '查询成功!'
						});
						this.total = res.data.total;
						this.UserShow = true;
						this.UserList = res.data.list;
						this.loading = false;
					}
				});
			},
			//添加用户
			addSubmit(){
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认添加该用户吗？', '提示', {}).then(() => {
							this.loading = true;
							var Token = sessionStorage.getItem('Token');
							var timestamp = new Date().getTime();
							let para = Object.assign({token: Token, timestamp:timestamp,}, this.addForm);
							addTreeUser(para).then((res) => {
								var res = res.data;
								if (res.code != 0) {
									this.$message({
										message: res.message,
										type: 'error'
									});
								} else {
									this.loading = false;
									this.$message({
										message: '添加成功',
										type: 'success'
									});
									this.$refs['addForm'].resetFields();
									this.adddialogVisible = false;
									this.getUser(this.addForm.treeId);
								}
							});
						});
					}
				});
			},
			// 删除用户
			delUser:function(index,row){
				this.$confirm('是否删除该用户?', '提示', {
				  confirmButtonText: '确定',
				  cancelButtonText: '取消',
				}).then(() => {
				  var Token = sessionStorage.getItem('Token');
				  var timestamp = new Date().getTime();
				  var para = {token: Token,timestamp: timestamp,ids:row.id};
				  delTreeUser(para).then((res) => {
				  	var res = res.data;
				  	if (res.code != 0) {
				  		this.$message({
				  			message: res.message,
				  			type: 'error'
				  		});
				  	} else {
				  		this.$message({
				  			type: 'success',
				  			message: '删除成功!'
				  		});
				  		this.loading = false;
				  		this.getUser(row.id);
				  	}
				  });
				}).catch(() => {
				  this.$message({
					type: 'info',
					message: '已取消'
				  });
				});
			},
			//分发红包
			Envelope(){
				this.$refs.Form.validate((valid) => {
					if (valid) {
						this.$confirm('确认发放分红吗？', '提示', {}).then(() => {
							this.loading = true;
							var Token = sessionStorage.getItem('Token');
							var timestamp = new Date().getTime();
							let para = {token: Token, timestamp:timestamp,redPackage:this.Form.money};
							getRedPackage(para).then((res) => {
								var res = res.data;
								if (res.code != 0) {
									this.$message({
										message: res.message,
										type: 'error'
									});
								} else {
									this.loading = false;
									this.$message({
										message: '发放成功',
										type: 'success'
									});
									this.$refs['Form'].resetFields();
									this.dialogVisible = false;
									this.TreeList();
								}
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			cellStyle({row, column}){
				return 'text-align:center;padding:5px;';
			},
			rowClass({row, column}){
				return 'text-align:center';
			},
			handleSizeChange(val) {
				this.size = val;
				this.getUser(this.id);
			},
			handleCurrentChange(currentPage) {
				this.page = currentPage;
				this.getUser(this.id);
			},
			handleClose(done) {
				this.$confirm('确认关闭？')
				    .then(_ => {
						done();
					})
				.catch(_ => {});
			},
			getUser:function(type){
				var Token = sessionStorage.getItem('Token');
				var timestamp = new Date().getTime();
				var para = {token: Token,timestamp: timestamp,treeId:type,curpage:this.page,pagesize:this.size};
				getTreeUserList(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.$message({
							type: 'success',
							message: '查询成功!'
						});
						this.total = res.data.total;
						this.UserShow = true;
						this.UserList = res.data.list;
						this.loading = false;
					}
				});
			},
		},
		
		beforeDestroy() {
		  clearInterval(this.timer);        
		  this.timer = null;
		}
	};
</script>

<style scoped>
.el-button.is-disabled{
	color: #bfcbd9;
	    cursor: not-allowed;
	    background-image: none;
	    background-color: #eef1f6;
	    border-color: #d1dbe5;
}
.el-pagination {
    white-space: nowrap;
    padding: 2px 5px;
    color: #48576a;
}
.el-select .el-input .el-input--suffix .el-input__inner {
    width: 130px !important;
  }  
</style>


